<?php

use app\common\library\wechat\WxClient;
use think\console\Input;
use think\console\Output;

/**
 * Created by PhpStorm.
 * User: xiewenfeng
 * Date: 2020/11/29
 * Time: 23:40
 */
class WxMsgCommand extends \think\console\Command
{
    protected function configure()
    {
        $this->setName('cron:wxmsg')->setDescription('this is a wx msg crontab!');
    }

    protected function execute(Input $input, Output $output)
    {
        \think\Log::info("微信消息队列：发送信息");
        $wxMsgModel = new \app\common\model\WxAppMsg();
        $msgs = $wxMsgModel->getUnsendMsgs();
        //处理中的提现
        if ($msgs) {
            foreach ($msgs as $msg) {
                \think\Log::info("信息内容：" . json_encode($msg));
                try {
                    $wxClient = new WxClient();
                    $wxClient->sendMessage(json_decode($msg->data), $msg->open_id, $msg->template_id, $msg->page);
                    $msg->msgSended();
                } catch (\Exception $e) {
                    \think\Log::error("发送失败：" . $e->getMessage());
                }
            }
        }
    }
}
